COMPILE=`date +"%F %T %z"` by `go version`
LDFLAGS=-ldflags "-w -s"

prod_name = douhu
CONFIG_FILE ?= ./config.json

.PHONY: build buildlinux clean swagger run dev test fmt vet tidy help

build: clean
	@echo "Building $(prod_name)..."
	@mkdir -p release/bin
	CGO_ENABLED=0 go build -o release/bin/$(prod_name) cmd/main.go
	@echo "Build completed: release/bin/$(prod_name)"

buildlinux: clean
	@echo "Building $(prod_name) for Linux..."
	@mkdir -p release/bin
	GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o release/bin/$(prod_name) cmd/main.go
	@echo "Linux build completed: release/bin/$(prod_name)"

clean:
	@echo "Cleaning build files..."
	@rm -rf release/bin/$(prod_name)

# 运行项目
run: build
	@echo "Starting $(prod_name) with config: $(CONFIG_FILE)"
	@./release/bin/$(prod_name) -c $(CONFIG_FILE)

# 开发模式运行
dev:
	@echo "Running in development mode with config: $(CONFIG_FILE)"
	@go run ./cmd/main.go -c $(CONFIG_FILE)

# 运行测试
test:
	@echo "Running tests..."
	@go test ./...

# 格式化代码
fmt:
	@echo "Formatting code..."
	@go fmt ./...

# 检查代码
vet:
	@echo "Vetting code..."
	@go vet ./...

# 整理依赖
tidy:
	@echo "Tidying dependencies..."
	@go mod tidy

swagger:
	ms_swag init -w -g cmd/main.go

# 显示帮助信息
help:
	@echo "Available targets:"
	@echo "  build      - Build the binary for current platform"
	@echo "  buildlinux - Build the binary for Linux"
	@echo "  run        - Build and run with config file"
	@echo "  dev        - Run in development mode (go run)"
	@echo "  clean      - Clean build files"
	@echo "  test       - Run tests"
	@echo "  fmt        - Format code"
	@echo "  vet        - Vet code"
	@echo "  tidy       - Tidy dependencies"
	@echo "  swagger    - Generate swagger docs"
	@echo "  help       - Show this help"
	@echo ""
	@echo "Usage examples:"
	@echo "  make run                         # Run with default config (./config.json)"
	@echo "  make run CONFIG_FILE=./dev.json  # Run with custom config"
	@echo "  make dev                         # Run in development mode"
